<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

</head>

<body>
  <div id="app">
    <common-head></common-head>
  </div>
  <script src="./vue.global.js"></script>
  <script src="https://unpkg.com/mitt/dist/mitt.umd.js"></script>
  <script>
    const { createApp } = Vue;
    const app = createApp({});
    // 创建一个第三方实例 emit和on方法
    const emitter = mitt();
    const CommonChild = {
      template: `
        <div>
          这是子组件1
          <button @click="emitMsg">向子组件2发送消息</button>
        </div>
      `,
      data(){
        return {
          msg: '这是子组件中1的数据'
        }
      },
      methods: {
        emitMsg() {
          emitter.emit('biubiu', this.msg);
        }
      }
    }
    const CommonChild2 = {
      template: `
        <div>
          这是子组件2
          {{ msg }}
        </div>
      `,
      data(){
        return {
          msg: ''
        }
      },
      mounted () {
        // 组件实例一个方法，会在组件初始化完成自动触发
        emitter.on('biubiu', (msg) => {
          console.log('我收到了兄弟', msg);
          this.msg = msg;
        })
      }
    }
    const CommonHead = {
      // 模板视图
      template: `
        <div>
          这是父组件
          <hr/>
          <common-child></common-child>
          <hr/>
          <common-child2></common-child2>
        </div>
      `,
      
      components: {
        CommonChild,
        CommonChild2
      }
    }
    app.component('CommonHead', CommonHead)

    app.mount('#app')
  </script>
</body>

</html>